<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Paper;

use App\Controller\Admin\AdminController;
use App\Model\UserPaperAnswer;
use App\Repositories\PaperRepository;
use App\Repositories\UserPaperAnswerRepository;
use App\Repositories\UserPaperRepository;
use App\Repositories\UserRepository;

class AnswerController extends AdminController
{
    public function list(){
        $wx_nickname = $this->request->input('wx_nickname','');
        $mobile = $this->request->input('mobile','');
        $district_id = $this->request->input('district_id','all');
        $street_id = $this->request->input('street_id','all');
        $community_id = $this->request->input('community_id','all');
        $rows = $this->request->input('rows',10);
        $order = [
            [
                'field' => 'id',
                'sort' => 'desc'
            ]
        ];
        $where=[];
        $join=[];
        if($wx_nickname!=''){
            $join['user_wechat']=[
                    'on' => ['user_paper.user_id','=','user_wechat.user_id'],
                    'where'=>[[
                        'field' => 'wx_nickname',
                        'value' => $wx_nickname,
                        'method' => 'like'
                    ]]
            ];
        }
        if($mobile!=''){
            $join['users']=[
                'on' => ['user_paper.user_id','=','users.id'],
                'where'=>[[
                    'field' => 'mobile',
                    'value' => $mobile,
                    'method' => 'like'
                ]]
            ];
        }
        if($district_id!='all'){
            $where['district_id'] =[
                'field' => 'district_id',
                'value' => $district_id
            ];
        }
        if($street_id!='all'){
            $where['street_id'] =[
                'field' => 'street_id',
                'value' => $street_id
            ];
        }
        if($community_id!='all'){
            $where['community_id'] =[
                'field' => 'community_id',
                'value' => $community_id
            ];
        }
        $datas = UserPaperRepository::getInstance()->getList('user_paper.*',$where,['limit'=>$rows],$order,$join,['paper']);
        if(!empty($datas['result'])){
            foreach ($datas['result'] as &$item){
                $item['sum_score'] = UserPaperAnswer::query()->where('user_paper_id',$item['id'])->sum('score');
                $item['user'] = UserRepository::getInstance()->getBaseInfo($item['user_id']);
            }
        }
        return $this->response->success($datas);
    }

    public function detail(){
        $id = $this->request->input('id',0);
        $user_paper = UserPaperRepository::getInstance()->getById($id);
        if(!$user_paper){
            return $this->response->error(100404,'数据不存在');
        }
        $where=[[
            'field' => 'user_paper_id',
            'value' => $id
        ]];
//        $join=[
//            'paper_question' => [
//                'on' => ['user_paper_answer.question_id','=','paper_question.id']
//            ]
//        ];
        $column = ['user_paper_answer.*'];
        $datas = UserPaperAnswerRepository::getInstance()->getList($column,$where,false,'',[]);
        foreach ($datas as &$item){
            $item->answers = json_decode($item->question_answers,true);
        }
        $user = UserRepository::getInstance()->getBaseInfo($user_paper->user_id);
        $paper = PaperRepository::getInstance()->getById($user_paper->paper_id);
        return $this->response->success([
           'questions' => $datas,
            'user' => $user,
            'paper' => $paper,
            'user_paper' => $user_paper
        ]);
    }

    public function delete(){
        $id = $this->request->input('id',0);
        UserPaperRepository::getInstance()->delete($id);
        UserPaperAnswerRepository::getInstance()->deleteAll([[
            'field' => 'user_paper_id',
            'value' => $id
        ]]);
        return $this->response->success([]);
    }
}